// pages/thief/steal/index.js
Page({
  data: {
    thiefId: 1,
    friendId: 1,
    thiefName: '调皮猴子',
    friendName: '好朋友',
    progress: 0,
    statusMessage: '',
    isCompleted: false,
    isSuccess: false,
    rewardPoints: 0,
    experienceGained: 0,
    showSuccessAnimation: false
  },

  onLoad(options) {
    const thiefId = options.thiefId ? parseInt(options.thiefId) : 1;
    const friendId = options.friendId ? parseInt(options.friendId) : 1;
    // 已移除未使用的变量thiefName和friendName
    
    // 模拟获取小偷和好友信息
    const thieves = [
      { id: 1, name: "调皮猴子", animation: "/animations/monkey.json" },
      { id: 2, name: "机智狐狸", animation: "/animations/fox.json" },
      { id: 3, name: "神秘猫咪", animation: "/animations/cat.json" },
      { id: 4, name: "快乐小狗", animation: "/animations/dog.json" }
    ];
    
    const friends = [
      { id: 1, name: "好朋友小李", mood: "开心", emoji: "😊" },
      { id: 2, name: "同事小王", mood: "兴奋", emoji: "🤩" },
      { id: 3, name: "邻居小张", mood: "平静", emoji: "😌" },
      { id: 4, name: "同学小赵", mood: "满足", emoji: "😌" }
    ];
    
    const thief = thieves.find(t => t.id === thiefId) || thieves[0];
    const friend = friends.find(f => f.id === friendId) || friends[0];
    
    this.setData({
      thiefId,
      friendId,
      thiefName: thief.name,
      friendName: friend.name,
      targetMood: {
        name: friend.mood,
        emoji: friend.emoji
      }
    });
  },

  onReady() {
    // 页面准备完成
  },

  onShow() {
    // 页面显示
  },

  onHide() {
    // 页面隐藏
  },

  onUnload() {
    // 页面卸载
  },

  onPullDownRefresh() {
    // 用户下拉动作
  },

  onReachBottom() {
    // 页面上拉触底事件
  },

  onShareAppMessage() {
    // 用户点击右上角分享
    return {
      title: '心情小偷',
      path: '/pages/thief/select/index'
    };
  },

  // 开始偷取进度
  startProgress() {
    let progress = 0;
    this.setData({ progress });
    
    const interval = setInterval(() => {
      progress += 5;
      let status;
      
      if (progress < 30) {
        status = `正在接近${this.data.friendName}...`;
      } else if (progress < 60) {
        status = `正在使用${this.data.thiefName}的技能...`;
      } else if (progress < 90) {
        status = '快要成功了...';
      } else {
        status = '即将完成...';
      }
      
      this.setData({
        progress,
        statusMessage: status
      });
      
      if (progress >= 100) {
        clearInterval(interval);
        this.completeSteal();
      }
    }, 200);
  },

  // 完成偷取
  completeSteal() {
    const isSuccess = Math.random() > 0.3; // 70%成功率
    const rewardPoints = isSuccess ? Math.floor(Math.random() * 50) + 10 : 0;
    const experienceGained = isSuccess ? Math.floor(Math.random() * 30) + 5 : 0;
    
    this.setData({
      isCompleted: true,
      isSuccess,
      rewardPoints,
      experienceGained,
      statusMessage: isSuccess 
        ? `成功！获得了${rewardPoints}个心情碎片和${experienceGained}点经验`
        : '很遗憾，没有成功偷取心情'
    });
    
    if (isSuccess) {
      this.showSuccessAnimation();
    }
  },

  // 显示成功动画
  showSuccessAnimation() {
    this.setData({
      showSuccessAnimation: true
    });
    
    setTimeout(() => {
      this.setData({
        showSuccessAnimation: false
      });
    }, 3000);
  },

  // 返回上一页
  goBack() {
    wx.navigateBack();
  },

  // 查看结果
  viewResult() {
    // 保存结果到本地存储
    const result = {
      success: this.data.isSuccess,
      rewardPoints: this.data.rewardPoints,
      experienceGained: this.data.experienceGained,
      thiefName: this.data.thiefName,
      friendName: this.data.friendName,
      targetMood: this.data.targetMood
    };
    
    wx.setStorageSync('last_steal_result', result);
    
    // 跳转到结果页面
    wx.redirectTo({
      url: '/pages/thief/result/index'
    });
  }
});